package com.gz.web.interceptor;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class MyInterceptor01 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //方法执行前执行
        System.out.println("====MyInterceptor01 preHandle====");
        return true; //返回false，不会执行目标方法 (前提是目标方法被拦截)
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //方法执行后执行
        //该方法可以获取到目标方法返回的ModelAndView
        System.out.println("====MyInterceptor01 postHandle====");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
       //视图渲染后被执行，可以进行资源清理工作
        System.out.println("====MyInterceptor01 afterCompletion====");
    }
}
